

function submitwant()
{
	cat = document.getElementById('input_category_id');
	desc = document.getElementById('textfield');
	
	$.ajax({
		url : base_url + "wants/submitwant",
		type : 'post',
		data :
		{
			category : cat.value,
			desc : desc.value,
		},
		dataType : 'json',
		success : function (response)
		{
			window.location.reload();
		}
	});
}

function submitsearch()
{
	c = document.getElementById('category_id');
	s = document.getElementById('searchfield');
	f = document.getElementById('filter');
	$.ajax({
		url : base_url+ "wants/search_result",
		type : 'post',
		data :
		{
			category : c.value,
			search : s.value,
			filter : f.value,
		},
		dataType : 'json',
		success : function (response)
		{
			searchresults = response;
			curr_page = 1;
			per_page = 9;
			num_pages = Math.ceil(searchresults.length / per_page);
			if(num_pages == 0){
				num_pages = 1;
			}
			
			makePaginator();
			DisplayPage(curr_page);
			positionPaginator();
			replaceIdByName();
			replaceIdByPic();
			
		}
	});
}

var selectedWant = -1;

function help(want)
{
	selectedWant = want;
	
	$.ajax({
		url : "wants/getrelatedoffers/",
			type : 'post',
			data :
			{
				want_id : want,
			},
			dataType : 'json',
			success : function(response) {
			
				html = '';
				if (response.length == 0){
					html = '<div class="medium-text">You have no related offer to suggest.</div>';
				}
				else
				{
					html= '<div id="offers-popup"><span class="medium-text">Select an existing offer</span><br />';
					
					html+= '<select id="offerselect">';
					for (var i = 0; i< response.length;i++)
					{
						html+= '<option value='+response[i].offer_id+'>'+response[i].item_title+'</option>';
					}

					html+= '</select>';
					html+= '<div id="submit-button-popup"><a href="Javascript:void(0);" onclick = "offersuggest()" ></a></div><br />';
					html+= '</div>';
				}
				var $dialog = $(html)
				.dialog({
					height: 210,
					width: 550,
					modal: 'true',
					title : 'Offer Suggestion',
					buttons : 
					{
						'Close' : function()
								{
									$(this).dialog('close');
								}
					}
				});
			}
	});
}

function offersuggest()
{
	var $dialog = $('<div></div>')
		.html('<span class="medium-text">Are you sure you want to suggest this offer?</span>')
		.dialog({
			modal: 'true',
			title : 'Confirm Suggestion',
			buttons :
			{
				'Yes' : function()
					{
						$.ajax({
							url : "wants/offersuggest/",
							type : 'post',
							data :
							{
								offer_id : $('#offerselect').val(),
								want_id : selectedWant,
							},
							success : function(response) {
								$('#offers-popup').html('<span class="medium-text">A notification has been sent to the person. Thanks for your help!</span>');
							}
						});
						$(this).dialog('close');
					},
				'No' : function()
					{
						$(this).dialog('close');
					},
			}
		});
}

function deletewant(want)
{
	var $dialog = $('<div></div>')
		.html('<span class="medium-text">Are you sure you want to delete this?</span>')
		.dialog({
			modal: 'true',
			title : 'Delete',
			buttons :
			{
				'Yes' : function()
					{
						$.ajax({
							url : "wants/delete/",
							type : 'post',
							data :
							{
								want_id : want,
							},
							success : function(response) {
								window.location.reload();
								$(this).dialog('close');
							}
						});
					},
				'No' : function()
					{
						$(this).dialog('close');
					},
			}
		});
}